package com.imooc.distributetransaction.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

@Configuration
public class AtomikosDataSource {

    @Value("${spring.datasource.druid.employee.url}")
    private String employeeUrl;

    @Value("${spring.datasource.druid.salary.url}")
    private String salaryUrl;

    @Value("${spring.datasource.druid.employee.username}")
    private String username;

    @Value("${spring.datasource.druid.employee.password}")
    private String password;

    @Bean(name = "employeeDataSource")
    public DataSource employeeDataSource(){
        AtomikosDataSourceBean atomikosDataSourceBean =new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("employeeDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");

        Properties properties = new Properties();
        properties.setProperty("URL",employeeUrl);
        properties.setProperty("user",username);
        properties.setProperty("password",password);
        atomikosDataSourceBean.setXaProperties(properties);

        return atomikosDataSourceBean;
    }

    @Bean(name = "salaryDataSource")
    public DataSource salaryDataSource(){
        AtomikosDataSourceBean atomikosDataSourceBean =new AtomikosDataSourceBean();
        atomikosDataSourceBean.setUniqueResourceName("salaryDataSource");
        atomikosDataSourceBean.setXaDataSourceClassName("com.mysql.cj.jdbc.MysqlXADataSource");

        Properties properties = new Properties();
        properties.setProperty("URL",salaryUrl);
        properties.setProperty("user",username);
        properties.setProperty("password",password);
        atomikosDataSourceBean.setXaProperties(properties);
        return atomikosDataSourceBean;

    }
}
